namespace ACRSAP.Issues.UserControls
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class RelatedIssues : UserControl, IIssueTab
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("CompareValidator1")]
        private CompareValidator _CompareValidator1;
        [AccessedThroughProperty("grdIssues")]
        private DataGrid _grdIssues;
        private int _IssueId;
        [AccessedThroughProperty("txtIssueId")]
        private TextBox _txtIssueId;
        private object designerPlaceholderDeclaration;

        public RelatedIssues()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.PreRender += new EventHandler(this.Page_PreRender);
            this._IssueId = 0;
        }

        public void AddRelatedIssue(object s, EventArgs e)
        {
            if ((StringType.StrCmp(this.txtIssueId.Text, string.Empty, false) != 0) && this.Page.IsValid)
            {
                RelatedIssue.CreateNewRelatedIssue(this._IssueId, int.Parse(this.txtIssueId.Text));
                this.txtIssueId.Text = string.Empty;
                this.BindRelated();
            }
        }

        public void BindRelated()
        {
            this.grdIssues.DataSource = RelatedIssue.GetRelatedIssues(this._IssueId);
            this.grdIssues.DataKeyField = "IssueId";
            this.grdIssues.DataBind();
        }

        public void grdIssuesItemCommand(object s, DataGridCommandEventArgs e)
        {
            int issueId = IntegerType.FromObject(this.grdIssues.DataKeys[e.Item.ItemIndex]);
            RelatedIssue.DeleteRelatedIssue(this._IssueId, issueId);
            this.BindRelated();
        }

        public void grdIssuesItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                RelatedIssue currentIssue = (RelatedIssue) e.Item.DataItem;
                Label lblIssueId = (Label) e.Item.FindControl("lblIssueId");
                lblIssueId.Text = currentIssue.IssueId.ToString();
            }
        }

        public void Initialize()
        {
            this.BindRelated();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_PreRender(object sender, EventArgs e)
        {
            if (this.Page.User.IsInRole("Guest"))
            {
                this.btnAdd.Enabled = false;
            }
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IIssueTab.IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddRelatedIssue);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddRelatedIssue);
                }
            }
        }

        protected virtual CompareValidator CompareValidator1
        {
            get
            {
                return this._CompareValidator1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._CompareValidator1 != null)
                {
                }
                this._CompareValidator1 = value;
                if (this._CompareValidator1 != null)
                {
                }
            }
        }

        protected virtual DataGrid grdIssues
        {
            get
            {
                return this._grdIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdIssues != null)
                {
                    this._grdIssues.ItemCommand -= new DataGridCommandEventHandler(this.grdIssuesItemCommand);
                    this._grdIssues.ItemDataBound -= new DataGridItemEventHandler(this.grdIssuesItemDataBound);
                }
                this._grdIssues = value;
                if (this._grdIssues != null)
                {
                    this._grdIssues.ItemCommand += new DataGridCommandEventHandler(this.grdIssuesItemCommand);
                    this._grdIssues.ItemDataBound += new DataGridItemEventHandler(this.grdIssuesItemDataBound);
                }
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                this._IssueId = value;
            }
        }

        protected virtual TextBox txtIssueId
        {
            get
            {
                return this._txtIssueId;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._txtIssueId != null)
                {
                }
                this._txtIssueId = value;
                if (this._txtIssueId != null)
                {
                }
            }
        }
    }
}

